<!DOCTYPE html>





<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.9.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-sunset.png?v=7.4.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-sunset.png?v=7.4.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-sunset.png?v=7.4.0">
  <link rel="mask-icon" href="/images/logo-sunset.svg?v=7.4.0" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.4.0">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.4.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: '',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="禁止使用System.out.print不要在代码上直接使用System.out或System.err 输出日志    System.out.println(&amp;quot;--&amp;gt; begin call sap finish service&amp;quot;); System.out.println(&amp;quot;--&amp;gt; call sap finish service status {}&amp;quot">
<meta name="keywords" content="开发管理">
<meta property="og:type" content="article">
<meta property="og:title" content="开发管理-日志规范">
<meta property="og:url" content="http://yoursite.com/2019/09/25/开发管理-日志规范/index.html">
<meta property="og:site_name" content="志泳的博客-学习、分享、成长">
<meta property="og:description" content="禁止使用System.out.print不要在代码上直接使用System.out或System.err 输出日志    System.out.println(&amp;quot;--&amp;gt; begin call sap finish service&amp;quot;); System.out.println(&amp;quot;--&amp;gt; call sap finish service status {}&amp;quot">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-10-23T15:28:02.987Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="开发管理-日志规范">
<meta name="twitter:description" content="禁止使用System.out.print不要在代码上直接使用System.out或System.err 输出日志    System.out.println(&amp;quot;--&amp;gt; begin call sap finish service&amp;quot;); System.out.println(&amp;quot;--&amp;gt; call sap finish service status {}&amp;quot">
  <link rel="canonical" href="http://yoursite.com/2019/09/25/开发管理-日志规范/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>开发管理-日志规范 | 志泳的博客-学习、分享、成长</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?aff336890e85bc8e76e40e879cca6bf8";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">
  <div class="container use-motion">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">志泳的博客-学习、分享、成长</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
            

          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
      <article itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block post">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/09/25/开发管理-日志规范/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Jimmy Wong">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="志泳的博客-学习、分享、成长">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">开发管理-日志规范

          
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2019-09-25 09:50:53" itemprop="dateCreated datePublished" datetime="2019-09-25T09:50:53+00:00">2019-09-25</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-10-23 15:28:02" itemprop="dateModified" datetime="2019-10-23T15:28:02+00:00">2019-10-23</time>
              </span>
            
          

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="禁止使用System-out-print"><a href="#禁止使用System-out-print" class="headerlink" title="禁止使用System.out.print"></a>禁止使用System.out.print</h2><p>不要在代码上直接使用System.out或System.err 输出日志    </p><pre><code>System.out.println(&quot;--&gt; begin call sap finish service&quot;);
System.out.println(&quot;--&gt; call sap finish service status {}&quot;+vo.getStatus());</code></pre><h2 id="统一使用-SLFJ"><a href="#统一使用-SLFJ" class="headerlink" title="统一使用 SLFJ"></a>统一使用 SLFJ</h2><p>我们在使用日志输出的时候，会考虑到以后会更换日志的实现，就如我们原来是用log4j，后来更换为log4j2，使用SLFJ，可以让我们的代码不用变更就能更换日志的框架。</p><a id="more"></a>

<pre><code>import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

***JAVA代码****
private static Logger LOGGER = LoggerFactory.getLogger(this.getClass());


***JAVA代码****
LOGGER.info(&quot;--&gt; call sap finish service status {}&quot;, vo.getStatus());</code></pre><h2 id="日志格式"><a href="#日志格式" class="headerlink" title="日志格式"></a>日志格式</h2><p>日志输出采用key，value的形式记录日志，key统一为大写，例如EVENT。EVENT记录的是，事件名称，为了方便后面定位问题，排错问题。key可以自行扩展，例如ORDER_ID=abc123456。</p>
<pre><code>log.info(String.format(&quot;EVENT=SAP采购订单序列化|CONTENT=%s&quot;, ToStringBuilder.reflectionToString(sapOrderList)));</code></pre><h2 id="采用MDC标注日志上下文"><a href="#采用MDC标注日志上下文" class="headerlink" title="采用MDC标注日志上下文"></a>采用MDC标注日志上下文</h2><p>MDC（Mapped Diagnostic Context，映射调试上下文）为了方便在多线程条件下记录日志的功能。当我们采用多线程的方式来处理用户的请求日志都打印在同一个文件上，比较难以区分不同请求下所对应的日志。当需要追踪某个用户在系统中的相关日志记录时，就会变得很麻烦。<br>使用MDC就是采用ThreadLocal的方式，给每一个线程一个mdcId，记录该线程下所有操作。</p>
<pre><code>import org.slf4j.MDC;

***java代码***
MDC.put(&quot;mdc-uuid&quot;, UUID.randomUUID().toString().replaceAll(&quot;-&quot;, &quot;&quot;));

***日志输出格式***
&lt;Property name=&quot;PATTERN_FORMAT&quot;&gt;%d{DEFAULT} [%thread:%X{mdc-uuid}] %-5level %logger{36} - %msg%n&lt;/Property&gt;
&lt;/Properties&gt;</code></pre><h2 id="不要在日志里面做字符串拼接"><a href="#不要在日志里面做字符串拼接" class="headerlink" title="不要在日志里面做字符串拼接"></a>不要在日志里面做字符串拼接</h2><p>采用字符串拼接的方式，其实不利于代码的可读性和可维护性，建议通过占位符的方式输入日志。</p>
<h2 id="保证日志输出的时候，不会出现异常"><a href="#保证日志输出的时候，不会出现异常" class="headerlink" title="保证日志输出的时候，不会出现异常"></a>保证日志输出的时候，不会出现异常</h2><p>其实不仅仅是日志的输出，我们在日常写代码的时候，也要注意不要输入异常。但是如果在日志的输出的地方造成异常，会导致日志的输出失败，而且没有相关的错误信息，会让排查错误造成困难。如下面的代码，如果vo为空，这个日志就会抛空指针异常。</p>
<pre><code>LOGGER.info(&quot;--&gt; call sap finish service status {}&quot;, vo.getStatus());</code></pre><h2 id="异常的输出，必须打印堆栈信息"><a href="#异常的输出，必须打印堆栈信息" class="headerlink" title="异常的输出，必须打印堆栈信息"></a>异常的输出，必须打印堆栈信息</h2><p>捕获到异常后，不能使用e.printStackTrace()，而是通过log error日志输出异常的所有的堆栈。</p>
<pre><code>} catch (Exception e) {
    LOGGER.error(&quot;EVENT=SAP采购订单序列化异常&quot;,e);     
}</code></pre>
    </div>

    
    
    
        
      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/开发管理/" rel="tag"># 开发管理</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/2019/09/25/Spring/" rel="prev" title="Spring提供的事件驱动模型(观察者模式)">
                  Spring提供的事件驱动模型(观察者模式) <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          
    
    <div class="comments" id="gitalk-container"></div>
  

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">
        
        
        
        
      

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#禁止使用System-out-print"><span class="nav-number">1.</span> <span class="nav-text">禁止使用System.out.print</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#统一使用-SLFJ"><span class="nav-number">2.</span> <span class="nav-text">统一使用 SLFJ</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#日志格式"><span class="nav-number">3.</span> <span class="nav-text">日志格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#采用MDC标注日志上下文"><span class="nav-number">4.</span> <span class="nav-text">采用MDC标注日志上下文</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#不要在日志里面做字符串拼接"><span class="nav-number">5.</span> <span class="nav-text">不要在日志里面做字符串拼接</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#保证日志输出的时候，不会出现异常"><span class="nav-number">6.</span> <span class="nav-text">保证日志输出的时候，不会出现异常</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#异常的输出，必须打印堆栈信息"><span class="nav-number">7.</span> <span class="nav-text">异常的输出，必须打印堆栈信息</span></a></li></ol></div>
        
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Jimmy Wong</p>
  <div class="site-description" itemprop="description"></div>
</div>
  <nav class="site-state motion-element">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">12</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">标签</span>
        
      </div>
    
  </nav>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Jimmy Wong</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.9.0</div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> v7.4.0</div>

        












        
      </div>
    </footer>
  </div>

  


  <script src="/lib/anime.min.js?v=3.1.0"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="/js/utils.js?v=7.4.0"></script><script src="/js/motion.js?v=7.4.0"></script>
<script src="/js/schemes/pisces.js?v=7.4.0"></script>

<script src="/js/next-boot.js?v=7.4.0"></script>



  





















  

  

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID: '1e03a8c2c942a007aa2d',
      clientSecret: 'c13db1d348fcbb2f0151ed1472c95119b9b34445',
      repo: 'blog-comment',
      owner: 'hzy2009',
      admin: ['hzy2009'],
      id: 'e0b9f1e7670195fa681205114eda076a',
        language: 'zh-CN',
      
      distractionFreeMode: 'true'
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
</script>

</body>
</html>
